<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ReviewerScoreProject extends Model
{
    protected $guarded = [];
    public $timestamps = false;

    const SCORE_STATUS_ID_WAIT_FOR_SCORE = 1;
    const SCORE_STATUS_ID_WAIT_FOR_SUBMIT = 2;
    const SCORE_STATUS_ID_SUBMITTED = 3;

    const SCORE_STATUS_PHRASE_WAIT_FOR_SCORE = '等待打分';
    const SCORE_STATUS_PHRASE_WAIT_FOR_SUBMIT = '已打分，等待提交';
    const SCORE_STATUS_PHRASE_SUBMITTED = '已提交';

    public function scoreStatus() {
        return self::getScoreStatus($this);
    }

    /**
     * @param $reviewerScoreProject ReviewerScoreProject|\stdClass|array 应包含属性：score 和 is_submitted
     * @return array 模式:
     * - score_status_id: int
     * - score_status_phrase: string
     */
    public static function getScoreStatus($reviewerScoreProject)
    {
        if (is_array($reviewerScoreProject)) {
            $reviewerScoreProject = (object)$reviewerScoreProject;
        }
        $scoreStatusId = $reviewerScoreProject->score === null ? self::SCORE_STATUS_ID_WAIT_FOR_SCORE :
            ($reviewerScoreProject->is_submitted ? self::SCORE_STATUS_ID_SUBMITTED : self::SCORE_STATUS_ID_WAIT_FOR_SUBMIT);
        $scoreStatusPhrase = self::getScoreStatusPhraseById($scoreStatusId);
        return [
            'score_status_id' => $scoreStatusId,
            'score_status_phrase' => $scoreStatusPhrase,
        ];
    }

    public static function getScoreStatusPhraseById($scoreStatusId)
    {
        $id2phrase = [
            self::SCORE_STATUS_ID_WAIT_FOR_SCORE => self::SCORE_STATUS_PHRASE_WAIT_FOR_SCORE,
            self::SCORE_STATUS_ID_WAIT_FOR_SUBMIT => self::SCORE_STATUS_PHRASE_WAIT_FOR_SUBMIT,
            self::SCORE_STATUS_ID_SUBMITTED => self::SCORE_STATUS_PHRASE_SUBMITTED,
        ];
        return $id2phrase[$scoreStatusId];
    }
}
